package allergies

import (
	"strconv"
)

const testVersion = 1

// 0 = 1, 1 = 2, 2 = 4 ... 7 = 128
var AllergiesCollections = []string{
	"eggs",
	"peanuts",
	"shellfish",
	"strawberries",
	"tomatoes",
	"chocolate",
	"pollen",
	"cats",
}

func Allergies(score uint) (allergies []string) {
	score = score % uint(256)
	if score == 0 {
		return
	}

	allergiesString := strconv.FormatUint(uint64(score), 2)
	idx := 0
	for i := len(allergiesString); i > 0; i-- {
		if allergiesString[i-1] == 49 {
			allergies = append(allergies, AllergiesCollections[idx])
		}
		idx++
	}

	return
}

func AllergicTo(i uint, allergen string) (r bool) {
	allergiesString := strconv.FormatUint(uint64(i), 2)
	idx := -1
	for i := 0; i < len(AllergiesCollections); i++ {
		if AllergiesCollections[i] == allergen {
			idx = i
		}
	}

	if idx+1 > len(allergiesString) {
		return
	}

	if allergiesString[idx] == 49 {
		r = true
	}

	return
}
